import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ModalController, LoadingController } from '@ionic/angular';
import { InstallService } from '@services/data.service/index';
import { DatePipe } from '@angular/common';
import { MyNoteService } from '@services/myNote.service';

@Component({
  selector: 'check-step',
  templateUrl: './check-step.html',
  styleUrls: ['check-step.scss']
})
export class CheckComponent implements OnInit {
  @Input() roomInfo;
  @Input() installData;
  @Input() Step;
  @Output() NextNum: EventEmitter<any> = new EventEmitter();
  click = false; // 防止重复点击
  CodeInfo = {
    CodeInfo1: null,
    CodeInfo2: null,
    CodeInfo3: null
  };
  constructor(public modalCtrl: ModalController, public installService: InstallService,
    private datePipe: DatePipe, private myNote: MyNoteService, private loadingCtrl: LoadingController) {
  }

  // 初始页面显示
  ngOnInit() {
    console.log('校核页面房间信息', this.roomInfo);
    this.Step.state = 1;
    let txt;
    const word = [];
    txt = this.installData.data1.split(';');
    for (let i = 0; i < txt.length; i++) {
      if (txt[i]) {
        txt[i] = txt[i].split(':');
        word.push({
          name: txt[i][0],
          value: txt[i][1]
        });
      }
    }
    this.CodeInfo.CodeInfo1 = word;
    this.CodeInfo.CodeInfo2 = this.installData.data5;
    setTimeout(() => {
      this.click = true;
    }, 2000);
  }

  // 切换页面
  async toTwo() {
    if (this.roomInfo.Type === 0) {
      this.Step.state = 2;
    } else {
      const loading = await this.loadingCtrl.create({
        spinner: 'bubbles',
        message: '正在进行校核...'
      });
      loading.present();
      this.installService.checkAgain(this.roomInfo.parameter.UID,
        this.roomInfo.device.meterId, this.installData.Address, this.roomInfo.device.pid).subscribe(res => {
          console.log('校核数据', res)
          if (res.success) {
            loading.dismiss();
            for (let i = 0; i < res.data.datas.length; i++) {
              if (res.data.datas[i].check === true) {
                res.data.datas[i].check = false;
              } else {
                res.data.datas[i].check = true;
              }
            }
            this.CodeInfo.CodeInfo3 = res.data.datas;
            this.Step.state = 2;
          } else {
            loading.dismiss();
            this.myNote.MyNotice(res.message);
          }
        });
    }
  }

  // 切换步骤
  toNext(i) {
    this.click = false;
    this.Step.now = i;
    const endTime = new Date();
    this.installData.step = 6;
    this.installData.endTime = this.datePipe.transform(endTime, 'yyyy-MM-dd HH:mm');
    this.installData.data6 = this.CodeInfo;
    this.installService.saveInstallData(this.installData.areaId, this.installData.meterId, this.installData);
    this.NextNum.emit(this.Step.now);
    setTimeout(() => {
      this.click = true;
    }, 1000);
  }

  // 刷新再次获取数据
  async freshCheck() {
    const loading = await this.loadingCtrl.create({
      spinner: 'bubbles',
      message: '正在刷新...'
    });
    loading.present();
    this.installService.checkAgain(this.roomInfo.parameter.UID,
      this.roomInfo.device.meterId, this.installData.Address, this.roomInfo.parameter.PID).subscribe(res => {
        if (res.success) {
          loading.dismiss();
          for (let i = 0; i < res.data.datas.length; i++) {
            if (res.data.datas[i].check === true) {
              res.data.datas[i].check = false;
            } else {
              res.data.datas[i].check = false;
            }
          }
          this.CodeInfo.CodeInfo3 = res.data.datas;
        } else {
          loading.dismiss();
          this.myNote.MyNotice(res.message);
        }
      });
  }
}
